package com.example.controller;


import com.example.model.entity.StudentEntity;
import com.example.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;

@RestController
@Slf4j
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    public Flux<StudentEntity> findAll() {
        return studentService.findAll();
    }

    @PutMapping("/student/{id}")
    public Mono<StudentEntity> update(@PathVariable("id") Long id) {
        Mono<StudentEntity> studentEntityMono = studentService.update(id);
        for (int i = 1; i < 1000; i++) {
            log.info("controller todo other things====" + i);
        }
        return studentEntityMono;
    }

    @GetMapping(value = "/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> see() {
        return studentService.findAll().delayElements(Duration.ofSeconds(1)).map(StudentEntity::getName);
    }
}
